package com.zhiyou100.blog.aciton;

import com.zhiyou100.blog.dao.TitleDao;
import com.zhiyou100.blog.entity.PageBean;
import com.zhiyou100.blog.entity.Title;
import com.zhiyou100.blog.entity.User;
import com.zhiyou100.blog.service.TitleService;
import com.zhiyou100.blog.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-10-23 8:48
 * <p>
 * 访问 / jQuery会失效
 */
@WebServlet("/init/message")
public class WelcomeServlet extends HttpServlet {
    private TitleService titleService = new TitleService();
    private UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("正在进行初始化容器 数据");
        //删除  init 初始化的数据
        //this.getServletContext().removeAttribute("init");

        request.getSession().setAttribute("init", "初始化成功");

        //向域中设置 标题分页的数据
        PageBean<Title> titlePage = titleService.getListTitlePaging(1);
        List<Title> topicList = titleService.getHotTopicList();
        List<User> startUserList = userService.getStartUserList();

        HttpSession session = request.getSession();

        session.setAttribute("titlePage", titlePage); //分页数据
        session.setAttribute("topicList", topicList); //热点话题
        session.setAttribute("startUserList", startUserList); //明星用户
//        request.getRequestDispatcher("/index.jsp").forward(request, response);
//        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
